package com.stars.entity;


import io.micrometer.core.instrument.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;


/**
 * @author Alex
 * @version 1.0
 * @description 分页工具类
 * @time 2016年11月9日下午2:55:45
 */
public class ListPage<T> implements Serializable {

    private static final long serialVersionUID = -4298435471058645262L;


    /**
     * 页记录数参数名
     **/
    @NotNull(message = "页记录数参数不能为空!")
    private Integer pageSize;

    /**
     * 页索引参数名
     **/
    @NotNull(message = "页索引参数不能为空")
    private Integer pageNow;

    /**
     * 排序列名
     **/
    private String sortName;

    /**
     * 排序方向
     **/
    private String sortOrder;

    private T entity;

    public ListPage() {
    }

    public ListPage(Integer pageSize, Integer pageNow, T entity) {
        this.pageSize = pageSize;
        this.pageNow = pageNow;
        this.entity = entity;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return
     * @description ligerUI初始分页pageNow=1,mysql分页LIMIT第一页为0，则需要减1
     * @author alex
     */
    public Integer getPageNow() {
        if (pageNow == null) {
            return 0;
        }
        return pageNow <= 1 ? 0 : (pageNow - 1) * pageSize;
    }

    public void setPageNow(Integer pageNow) {
        this.pageNow = pageNow;
    }

    public String getSortName() {
        if (StringUtils.isBlank(this.sortName)) {
            this.sortName = null;
        }
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        if (StringUtils.isBlank(this.sortOrder)) {
            this.sortOrder = null;
        }
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }
}
